package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.feign.search.SearchFeignClient;
import com.atguigu.gmall.model.search.vo.SearchParamsVO;
import com.atguigu.gmall.model.search.vo.SearchResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Arrays;

@Controller
public class SearchListController {

    @Autowired
    SearchFeignClient searchFeignClient;

    @GetMapping("/list.html")
    public String searchList(SearchParamsVO param, Model model) {
        //远程调用检索服务
        if (param.getProps() != null && param.getProps().length > 0) {
            String[] array = Arrays.stream(param.getProps()).map(item -> {
                item = item.replace(" ", "+");
                return item;
            }).toArray(String[]::new);
            param.setProps(array);
        }

        Result<SearchResultVo> search = searchFeignClient.search(param);
        SearchResultVo data = search.getData();


        //分析页面的数据
        //==== 检索参数
        model.addAttribute("searchParam",data.getSearchParam());


        //==== 面包屑区=====
        //品牌面包屑
        model.addAttribute("trademarkParam",data.getTrademarkParam());
        //属性面包屑
        model.addAttribute("propsParamList",data.getPropsParamList());


        //==== 二次检索区 ===
        //品牌列表
        model.addAttribute("trademarkList",data.getTrademarkList());
        //属性列表
        model.addAttribute("attrsList",data.getAttrsList());


        //==== 排序、分页数据===
        model.addAttribute("orderMap",data.getOrderMap());
        model.addAttribute("pageNo",data.getPageNo());
        model.addAttribute("totalPages",data.getTotalPages());
        model.addAttribute("urlParam",data.getUrlParam());


        //==== 商品数据区
        model.addAttribute("goodsList",data.getGoodsList());




        return "list/index";
    }
}
